package com.ysxsoft.wdg.ui.activity.center_zy;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.TabAdapter;
import com.ysxsoft.wdg.adapter.home.ZyGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BannerBean;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.zy.GoodsBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 购物精选
 */
public class ZyHotActivity extends BaseActivity implements OnRefreshListener,OnLoadmoreListener{


    public static void start(Context context){
        Intent intent = new Intent(context,ZyHotActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private TextView[] textViews;

    private TabAdapter tabAdapter;
    private ZyGoodsAdapter mAdapter;

    private String categoryid = "",uid ="";
    private int page = 1,xu =0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tb_jx;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("热门商品");

        uid = SharePrefUtils.getUserId();

        titleIvR.setImageResource(R.mipmap.zy_search);
        textViews = new TextView[]{tv0,tv1,tv2,tv3};

        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
        tabAdapter = new TabAdapter(R.layout.item_tab);
        recyclerview.setAdapter(tabAdapter);

        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new ZyGoodsAdapter(R.layout.item_goods);
        recyclerview2.setAdapter(mAdapter);

        multStatusView.showLoading();
        getTab();
    }

    private void getTab() {
        mApiHelper.getZyHotCate(new Observer<BaseBean<List<BannerBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<BannerBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    tabAdapter.setNewData(baseBean.getData());
                    tabAdapter.setSelect(0);
                    categoryid = baseBean.getData().get(0).getId();
                    setSelect(0);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);

        tabAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                categoryid = tabAdapter.getItem(position).getId();
                tabAdapter.setSelect(position);
                setSelect(0);
            }
        });


        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsDetailActivity.start(mContext,mAdapter.getItem(position).getId());
            }
        });

    }


    public void setTabSelect(int tabPosition){
        tabAdapter.setSelect(tabPosition);

    }

    @OnClick({R.id.title_finish, R.id.title_iv_r})
    public void onViewClicked0(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_iv_r:
                ZySearchActivity.start(mContext);
                break;
        }
    }

    //xu  	1销量2最新3到手价倒序4到手价正序
    @OnClick({ R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 0;
                setSelect(0);
                break;
            case R.id.tv1:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 1;
                setSelect(1);
                break;
            case R.id.tv2:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 2;
                setSelect(2);
                break;
            case R.id.tv3:
                xu = (xu == 3) ? 4 : 3;
                iv3.setImageResource((xu == 3) ?R.mipmap.xu_down : R.mipmap.xu_up);
                setSelect(3);
                break;
        }


    }

    private void setSelect(int position) {
        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setSelected(i==position);
        }
        onRefresh(refreshLayout);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(500);

        page++;
        getList();

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);

        page = 1;
        mAdapter.clear();
        getList();
    }

    private void getList() {
        mApiHelper.getZyHotList(page + "", categoryid, "",xu + "",uid, new Observer<BaseBean<List<GoodsBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<GoodsBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    multStatusView.showContent();
                    if (page == 1){
                        mAdapter.setNewData(baseBean.getData());
                    }else {
                        mAdapter.addData(baseBean.getData());
                    }
                }else {
                    if (page == 1){
                        multStatusView.showEmpty();
                    }else {
                        multStatusView.showContent();
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
